/*
 *	ncp_hr_collector_app.h
 *
 *	Created on: 2018年4月4日
 *	Author: NKJ
 */

#ifndef NCP_HR_COLLECTOR_APP_H
#define NCP_HR_COLLECTOR_APP_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

#define ATHEADER 	"AT+"
#define ATEND 		"\r\n"

#define BLE_GAP_AD_TYPE_FLAGS                               0x01 /**< Flags for discoverability. */
#define BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE   0x02 /**< Partial list of 16 bit service UUIDs. */
#define BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE         0x03 /**< Complete list of 16 bit service UUIDs. */
#define BLE_GAP_AD_TYPE_32BIT_SERVICE_UUID_MORE_AVAILABLE   0x04 /**< Partial list of 32 bit service UUIDs. */
#define BLE_GAP_AD_TYPE_32BIT_SERVICE_UUID_COMPLETE         0x05 /**< Complete list of 32 bit service UUIDs. */
#define BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE  0x06 /**< Partial list of 128 bit service UUIDs. */
#define BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE        0x07 /**< Complete list of 128 bit service UUIDs. */
#define BLE_GAP_AD_TYPE_SHORT_LOCAL_NAME                    0x08 /**< Short local device name. */
#define BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME                 0x09 /**< Complete local device name. */
#define BLE_GAP_AD_TYPE_TX_POWER_LEVEL                      0x0A /**< Transmit power level. */
#define BLE_GAP_AD_TYPE_CLASS_OF_DEVICE                     0x0D /**< Class of device. */
#define BLE_GAP_AD_TYPE_SIMPLE_PAIRING_HASH_C               0x0E /**< Simple Pairing Hash C. */
#define BLE_GAP_AD_TYPE_SIMPLE_PAIRING_RANDOMIZER_R         0x0F /**< Simple Pairing Randomizer R. */
#define BLE_GAP_AD_TYPE_SECURITY_MANAGER_TK_VALUE           0x10 /**< Security Manager TK Value. */
#define BLE_GAP_AD_TYPE_SECURITY_MANAGER_OOB_FLAGS          0x11 /**< Security Manager Out Of Band Flags. */
#define BLE_GAP_AD_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE     0x12 /**< Slave Connection Interval Range. */
#define BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_16BIT       0x14 /**< List of 16-bit Service Solicitation UUIDs. */
#define BLE_GAP_AD_TYPE_SOLICITED_SERVICE_UUIDS_128BIT      0x15 /**< List of 128-bit Service Solicitation UUIDs. */
#define BLE_GAP_AD_TYPE_SERVICE_DATA                        0x16 /**< Service Data. */
#define BLE_GAP_AD_TYPE_PUBLIC_TARGET_ADDRESS               0x17 /**< Public Target Address. */
#define BLE_GAP_AD_TYPE_RANDOM_TARGET_ADDRESS               0x18 /**< Random Target Address. */
#define BLE_GAP_AD_TYPE_APPEARANCE                          0x19 /**< Appearance. */
#define BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA          0xFF /**< Manufacturer Specific Data. */

#define BLE_16BIT_UUID      (16)
#define BLE_32BIT_UUID      (32)
#define BLE_128BIT_UUID     (128)

#define SCANNING       		1
#define WILL_FIND_SERVICE   2
#define FIND_SERVICE   		3
#define FIND_CHAR      		4
#define ENABLE_NOTIF   		5
#define DATA_MODE      		6

#define MAX_CONNECTIONS (1)
#define MAX_ADVERTISERS (2)

#define USART0_DMA_RX_FIFO_LEN                  (256)
#define ATCMDBUFFLEN                            (128)
#define MTU_LEN	                                (247)

#define MAX_SERVICE_UUID_COUNTS                 (3)
#define MAX_EVERY_SERVICE_CHARACT_UUID_COUNTS   (3)

#define CONNCB_DEFAULT     {    .mac.addr = {0}, \
                                .connectedid = 0xFF, \
                                .mainservice = 1, \
                                .discoverserviceindex = 0, \
                                .service_uuid = {"\x00\x18","\x0D\x18","\x0F\x18"}, \
                                .service_uuid_bit = {16,16,16}, \
                                .charact_uuid = {{"\x00\x2A",0},{"\x37\x2A",0},{"\x19\x2A",0}}, \
                                .charact_uuid_bit = {{16,0},{16,0},{16,0}}, \
                                .service_handle = {0}, \
                                .charact_handle = {0}, \
                                .charact_properties = {0}, \
                                .discoverstate = SCANNING, \
                                .bondsta = DISABLE, \
                                .pin = 1234, \
                                .discontime = 0, \
                            }

#define BLECB_DEFAULT       {   .local_mac.addr = {0}, \
                                .txpower = 100, \
                                .pin = 1234, \
#ifdef EFR_IM_SOC   \
                                .appsta = STOP, \
                                .advsta = STOP, \
                                .scansta = STOP, \
                                .workstyle = CLIENT, \
                                .atcmdbuff = {0}, \
                                .usart0dmachannel = 0, \
                                .usart0dmarxfifo = {0}, \
#endif              \
                                .scanfilter = {0}, \
                                .scanfilterlen = 0, \
                                .connindex = 0, \
                                .connections = 0, \
                                .conncb[0] = CONNCB_DEFAULT, \
                            }

PACKSTRUCT(
typedef struct _conncb 
{
    bd_addr mac;
    uint8_t connectedid;
    uint8_t mainservice;
    uint8_t discoverserviceindex;
    uint8_t *service_uuid[MAX_SERVICE_UUID_COUNTS];
    uint8_t service_uuid_bit[MAX_SERVICE_UUID_COUNTS];
    uint8_t *charact_uuid[MAX_SERVICE_UUID_COUNTS][MAX_EVERY_SERVICE_CHARACT_UUID_COUNTS];
    uint8_t charact_uuid_bit[MAX_SERVICE_UUID_COUNTS][MAX_EVERY_SERVICE_CHARACT_UUID_COUNTS];
    uint32_t service_handle[MAX_SERVICE_UUID_COUNTS];
    uint32_t charact_handle[MAX_SERVICE_UUID_COUNTS][MAX_EVERY_SERVICE_CHARACT_UUID_COUNTS];
    uint8_t charact_properties[MAX_SERVICE_UUID_COUNTS][MAX_EVERY_SERVICE_CHARACT_UUID_COUNTS];
    uint8_t discoverstate;
    uint8_t bondsta;
    uint32_t pin;
    uint32_t discontime;
})ConnCB,*pConnCB;

PACKSTRUCT(
typedef struct _blecb 
{
    bd_addr local_mac;
    uint8_t txpower;
    uint32_t pin;
#ifdef EFR_IM_SOC
    uint8_t appsta;
    uint8_t advsta;
    uint8_t scansta;
    uint8_t workstyle;
    uint8_t atcmdbuff[ATCMDBUFFLEN];
    uint16_t usart0dmachannel;
    uint8_t  usart0dmarxfifo[USART0_DMA_RX_FIFO_LEN];
#endif
    uint8_t scanfilter[32];
    uint8_t scanfilterlen;
    uint8_t connindex;
    uint8_t connections;
    ConnCB conncb[MAX_CONNECTIONS];
})BleCB,*pBleCB;

enum
{
	STOP = 0,
	START= 1,
};

enum
{
    ePROPERTIES_Broadcast                    = 0x01,
    ePROPERTIES_Read                         = 0x02,
    ePROPERTIES_WriteWithoutResponse         = 0x04,
    ePROPERTIES_Write                        = 0x08,
    ePROPERTIES_Notify                       = 0x10,
    ePROPERTIES_Indicate                     = 0x20,
    ePROPERTIES_AuthenticatedSignedWrites    = 0x40,
    ePROPERTIES_ExtendedProperties           = 0x80,   
};

enum
{
	ADVER = 0,
	CLIENT= 1,
	SERVE = 2,
    ADVER_SERVE = 3,

    SHUTDOWN = 4,

    STYLELAST,
};

enum
{
	DISABLE = 0,
	ENABLE  = 1,
};

enum timer
{
	TIMER_ADV_STOP = 1,
    TIMER_ADV_START,
	TIMER_SCAN_START,
	TIMER_SCAN_STOP,
	TIMER_GATT_SEND,
	TIMER_CON_START,
	TIMER_RERAD_CHARACT,
	
	TIMER_LED,
};

enum _external_signal
{
	EXSG_WAKEUP = 1,
	EXSG_USART0_RX_DMA_DONE,
	EXSG_USART0_RX_DMA_TIMEOUT,
	EXSG_RECV_USR_ADV_DATA,
	EXSG_RECV_USR_AT_CMD,
};

enum _at_cmd_id
{
	ATCMD_NONE = 0x00,
	ATCMD_HEADER,
	ATCMD_END,
	ATCMD_PLUS,
	ATCMD_DFU,
	ATCMD_QUERY_MAC,
	ATCMD_QUERY_BLEWORKSTYLE,
	ATCMD_SET_BLEWORKSTYLE,
	ATCMD_QUERY_VERSION,
	ATCMD_SCANSTART,
	ATCMD_SCANSTOP,
	ATCMD_SET_DATAFILTER,
	ATCMD_GOTO_SLEEP,
	ATCMD_SET_NAME,
	ATCMD_QUREY_CONSTA,
	ATCMD_SET_HR,
    ATCMD_SET_BATLEVEL,
	ATCMD_SET_BATVOLTAGE,
	ATCMD_SET_BATCURRENT,
    ATCMD_SET_PIN,
    ATCMD_DISCON,
    ATCMD_QUERY_ALL,
    ATCMD_SHUTDOWN,
    ATCMD_CONNECT,

	ATCMD_ERROR,
};

enum _msgid
{
    RETURN_MSG_ATCMD_NONE = 0x00,
	RETURN_MSG_ATCMD_HEADER,
	RETURN_MSG_ATCMD_END,
	RETURN_MSG_ATCMD_PLUS,
	RETURN_MSG_ATCMD_DFU,
	RETURN_MSG_ATCMD_QUERY_MAC,
	RETURN_MSG_ATCMD_QUERY_BLEWORKSTYLE,
	RETURN_MSG_ATCMD_SET_BLEWORKSTYLE,
	RETURN_MSG_ATCMD_QUERY_VERSION,
	RETURN_MSG_ATCMD_SCANSTART,
	RETURN_MSG_ATCMD_SCANSTOP,
	RETURN_MSG_ATCMD_SET_DATAFILTER,
	RETURN_MSG_ATCMD_GOTO_SLEEP,
	RETURN_MSG_ATCMD_SET_NAME,
	RETURN_MSG_ATCMD_QUREY_CONSTA,
	RETURN_MSG_ATCMD_SET_HR,
    RETURN_MSG_ATCMD_SET_BATLEVEL,
	RETURN_MSG_ATCMD_SET_BATVOLTAGE,
	RETURN_MSG_ATCMD_SET_BATCURRENT,
    RETURN_MSG_ATCMD_SET_PIN,
    RETURN_MSG_ATCMD_DISCON,
    RETURN_MSG_ATCMD_QUERY_ALL,
    RETURN_MSG_ATCMD_SHUTDOWN,
    RETURN_MSG_ATCMD_CONNECT,
	RETURN_MSG_ATCMD_ERROR,

    RETURN_MSG_SYS_BOOT = 0x80,
    RETURN_MSG_OPENED5,
    RETURN_MSG_OPENED4,
    RETURN_MSG_CLOSED,
    RETURN_MSG_NOTIFY_EN,
    RETURN_MSG_NOTIFY_DIS,
    RETURN_MSG_BONDED,
    RETURN_MSG_OVERFLOW,
    RETURN_MSG_DATAERROR,
    RETURN_MSG_WORKSTYLE,
    RETURN_MSG_MASTER_SERVE,
    RETURN_MSG_MASTER_NOTIFY,
    RETURN_MSG_MASTER_DATA,
};

typedef union
{
	uint8_t raw_data[31];
	struct
	{
		uint8_t header[8];
		uint8_t data_len;  		//数据长度
		uint8_t data_mode;  	//数据类型
        uint16_t company_id;    //厂商ID
		uint8_t data[19];  		//数据
	}frame;
}ubrddata,*pubrddata;


void appHandleEvents(struct gecko_cmd_packet *evt);


#ifdef __cplusplus
};
#endif

#endif /* APP_H */
